<template>
  <header>
    <div class="banxin tanxin header_box">
      <section>欢迎来到叩丁狼积分商城</section>
      <ul>
        <li class="avater_li">
          <img width="26" :src="userInfo.headImg" alt="" />
          用户名 ：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li v-if="isLogin" class="header_btn cart_btn">
          <img src="../assets/images/Shopping.png" alt />
          <span @click="$router.push('/user/cart')">购物车</span>
          <b>{{ cartTotal }}</b>
        </li>
        <li class="header_btn login_btn" v-else @click="goLogin">登录</li>
        <li class="header_btn login_btn" @click="outLogin">退出登录</li>
        <li class="header_btn login_btn" @click="jiebangWXfn">解绑微信</li>
      </ul>
    </div>
  </header>
</template>

<script>
import { UserInfoApi, WeixinLoginApi, UnbindWechatPhoneApi, } from "@/request/api";
export default {
  data() {
    return {
      isLogin: false,
      // 用户信息
      userInfo: {
        // 用户头像
        headeImg: require("../assets/images/userImg.png"),
        // 用户名
        nickName: "游客",
        // 积分数
        coin: "--",
        // 购物车商品数量
        cartTotal: 0
      }
    };
  },
  methods: {
    // 解绑微信
    jiebangWXfn() {
      UnbindWechatPhoneApi().then(res => {
        let toastObj = {
              content: res.data.data,
              icon: "danger",
            };
            this.$store.dispatch("AsyncToast", toastObj);
      });
    },
    goLogin() {
      // 显示登录的模态框 ，打开组件
      // 修改vuex当中的 isOpenLogin 的值 ， 触发 mutations 去打开 login 模态框
      this.$store.commit("changeIsLogin", true);
    },
    outLogin() {
      localStorage.removeItem("x-auth-token");
      this.$router.go(0);
    },
    // 公共函数
    bindWatchFn() {
      // console.log(this.$route.query.code);
      // 如果地址栏 存在 微信的code  就去请求微信登录的接口
      // 如果存在code 才去请求 否则不做任何请求
      let code = this.$route.query.code;
      if (code) {
        WeixinLoginApi({ code }).then(res => {
          // console.log(res);
          if (res.data.code === 0) {
            // 成功
            let toastObj = {
              content: "登录成功",
              icon: "success"
            };
            this.$store.dispatch("AsyncToast", toastObj);
            // 存储token
            localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
            //  更新header组件
            this.$emit("updateKeyFn");
            // 删除地址栏的code
            this.$router.push(this.$route.path);
          } else if (res.data.code === 400) {
            // 过期了 重新扫码
            let toastObj = {
              content: "页面过期 请重新扫描二维码",
              icon: "danger"
            };
            this.$store.dispatch("AsyncToast", toastObj);
            // 重新打开登录框
            this.$store.commit("changeIsLogin", true);
          } else if (res.data.code === 407) {
            // 需要手机号绑定微信
            let toastObj = {
              content: "请使用手机号绑定微信后再使用",
              icon: "danger"
            };
            this.$store.dispatch("AsyncToast", toastObj);
            // 重新打开登录框
            this.$store.commit("changeIsLogin", true);
            // 存uuid -- 临时存储 因为这个东西 很快会过期
            sessionStorage.setItem("uuid", res.data.uuid);
          } else {
            console.log('错');
          }
        });
      }
    },
  },
  created() {
    UserInfoApi().then(res => {
      if (res.data.code === 0) {
        let { userInfo, cartTotal } = res.data.data;
        this.userInfo = userInfo;
        this.isLogin = true;
        this.cartTotal = cartTotal;
      }
      // console.log(this.userInfo);
    });
    // 调用监听函数
    this.bindWatchFn();
  }
};
</script>
 
<style lang = "less" scoped>
@import "../assets/base.less";

header {
  background: @black;
  height: 40px;
  color: #fffefe;
  font-size: 14px;
  .header_box {
    align-items: center;
    height: 40px;
    ul {
      display: flex;
      height: 40px;
      align-items: center;
      li {
        margin-left: 20px;
        cursor: pointer;
        &.avater_li {
          display: flex;
          height: 26px;
          align-items: center;
          img {
            border-radius: 50%;
            margin-right: 8px;
          }
        }
        &.header_btn {
          width: 124px;
          height: 40px;
          background: @blue;
        }
        &.cart_btn {
          display: flex;
          align-items: center;
          justify-content: center;
          span {
            margin: 0 8px;
          }
          b {
            width: 22px;
            height: 22px;
            background: @orange;
            border-radius: 50%;
            text-align: center;
            line-height: 22px;
          }
        }
        &.login_btn {
          text-align: center;
          line-height: 40px;
        }
      }
    }
  }
}
</style>